var express = require('express');
var router = express.Router();

const mongoose = require("mongoose");

const { login2,getadmins2,updateOne2, deleteOne2,addOne2} = require('../service/adminsService');

//获取管理员列表
router.get('/getlist', async function(req, res) {
  res.send(await getadmins2(req.query));
});
//更新修改状态
router.put('/', async function(req, res) {
  
   res.send(await updateOne2(req.body));
  
});

//删除
router.delete('/',async (req, res) =>{
  res.send(await deleteOne2(req.body))
})
//增加
router.post('/addOne', async (req, res) => {
  res.send(await addOne2(req.body))
  
})
// 登陆
router.post('/login', async (req, res) => {
  const result =await login2(req.body)
  req.session.regenerate(function() {
      req.session.loginAdmin = req.body.adminname
      res.send(result);
    })
})
//判断是否登陆
router.get('/islogin', (req, res) => {
  const admin =req.session.loginAdmin
  if(admin) {
    res.send({isLogin:true,admin})
  } else {
    res.send({isLogin:false})
  }
})

//登出
router.get('/logout', (req, res) => {
  req.session.destroy(function() {
    res.clearCookie('currentUser')  //从app.js里name对象里找
    res.send(true)
  })
})

module.exports = router;